<?php
namespace Home\Controller;
use Think\Controller;
class CategoryController extends Controller {
    public function index(){
    	$c = D("category");
    	$items = $c->select();
    	// echo ($cao);
    	//dump($items);
    	// $this->assign('items',$items);
      //   $this->display();
        $this->ajaxReturn($items);
    }
    public function Del($id=0){
      if($id==0){
        $this->error('删除失败,请选择分类ID!');
      }
      $a = D('article');
      $a_count = $a->where(array('category_id'=>$id))->count();
      //$a->where(array('category_id',$id))->count();  
      if($a_count>0){
         $this->error('删除失败,请先删除分类下的文章!');
    }else{
         $c = D("category");
         if($c->delete($id)){
            $this->success('删除成功');
          }else{
            $this->error('删除失败');
         }
    }
     
      
    }
    public function save($id = -1){
    	$c = D("category");
        if(IS_POST){
        	
        	  $cata=$c->create();
        	 //echo "**".$id.'88';
        	if($id==-1||$id==""){
        	 	echo "-1";
               $id =  $c->add();
               //$this->redirect('Save',array('id' => $id), 0, '888...'); 
       
        	 }else{
        	 		//echo "-551";\
        	 	//	dump($c->create());
               $c->save();
        	 }
        	
        	
        	   $this->redirect('Index','', 0, '跳转中...'); 
       
        }else{
        	$cata = ($c->where(array('id'=>$id))->find());
          // echo 'po';
        }
    	//dump($cata);
    	//$id = $c->add();
    	
    	//echo $c->create(); 
    	//$c
       $this->assign($cata);
       $this->display();
    }
}